package campus.aiit.ssl.service;

import campus.aiit.ssl.dao.DeviceModelDao;
import campus.aiit.ssl.dao.DeviceTypeDao;
import campus.aiit.ssl.domain.model.DeviceModel;
import campus.aiit.ssl.domain.model.DeviceType;
import campus.aiit.ssl.manage.custom.dao.DaoProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author ihaokun
 * @date 2019/12/3 15:41
 */
@Service
public class DeviceAttributesService {
  private DeviceTypeDao typeDao;
  private DeviceModelDao modelDao;

  @Autowired
  public DeviceAttributesService(DeviceTypeDao typeDao, DeviceModelDao modelDao) {
    this.typeDao = typeDao;
    this.modelDao = modelDao;
  }
  @PostConstruct
  public void afterConstruct(){
    DaoProxy<DeviceTypeDao> foo = new DaoProxy<>();
    typeDao = foo.getProxy(typeDao);
    DaoProxy<DeviceModelDao> bar = new DaoProxy<>();
    modelDao = bar.getProxy(modelDao);
  }

  public String getType(Long id){
    return typeDao.selectNameByPrimaryKey(id);
  }

  public boolean addType(String name){
    typeDao.insert(name);
    return true;
  }

  public boolean removeType(long id){
    typeDao.deleteByPrimaryKey(id);
    return true;
  }

  public boolean modifyType(DeviceType type){
    typeDao.updateByPrimaryKeySelective(type);
    return true;
  }

  public DeviceType[] types(){
    return typeDao.selectAll();
  }

  public DeviceModel[] models(){
    return modelDao.selectALl();
  }

  public boolean addModel(DeviceModel model){
    modelDao.insertSelective(model);
    return true;
  }

  public boolean modifyModel(DeviceModel model){
    modelDao.updateByPrimaryKeySelective(model);
    return true;
  }

  public boolean removeModel(long id){
    modelDao.deleteByPrimaryKey(id);
    return true;
  }

  public String[] brands() {
    return modelDao.selectBrands();
  }
  public DeviceModel[] models(String name){
    return modelDao.selectByBrandName(name);
  }

  public Long getMaxId(){return typeDao.selectMaxId();}
}